package com.hys.authserver.api;

import com.hys.authserver.api.vo.AuthReq;
import com.hys.authserver.auth.AuthenticationService;
import com.hys.hou.common.base.data.R;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/api/auth")
public class AuthController {

    @Resource
    private AuthenticationService authenticationService;

    @PostMapping("/signin")
    public R authenticate(@RequestBody R<AuthReq> authenticationRequest) {
        try {
            return R.success(authenticationService.authenticate(authenticationRequest.getData()));
        } catch (Exception e) {
            log.error("登录失败:{}",e.getMessage());
            return R.client("登录失败");
        }
    }
    @PostMapping("/logout")
    public R logout(@RequestBody R<AuthReq> authenticationRequest) {
        try {
            authenticationService.logout(authenticationRequest.getData().getUsername());
            return R.success();
        } catch (Exception e) {
            log.error("注销失败:{}",e.getMessage());
            return R.client("注销失败");
        }
    }
}
